<?php
ls_import('lslib/Error');
ls_import('lslib/DataObject');
ls_import('lslib/SqlQueryBuilder');

class User extends DataObject
{
	var $perm ;
	var $role ;
	
	function User(&$context)
	{
		parent::DataObject(&$context);
		$this->data = array();
		$this->perm = array();
	}
	
	function getId ()
	{
		$this->get($this->context->config['DatabaseColumn']['User']['UserID']);
	}
	
	function setPassword($newPassword='')
	{
		$this->set($this->context->config['DatabaseColumn']['User']['UserPassword'],md5($newpassword));
	}
	
	function setRandomPassword(){
		$keyWord = array("Z" , "C" , "6" , "V" , "z" , "x" , "c" , "v"  , "M" , "r" , "t" , "y" , "1"  , "B" , "N", "o" , "w" , "7", "p"  , "F" , "G" , "f" , "g" , "h" , "a", "5" , "I" , "O" , "P" , "A", "q" , "H" , "J" , "K" , "8"  , "L" , "u" , "i"  , "s" , "d" , "9" , "0" , "4" , "Q" , "W" , "E" , "R" , "T" , "Y" , "U" , "e"  , "S" , "D", "j" ,"2" , "3", "X" ,  "k" , "l" , "b" , "n" , "m");
		$Max = count($keyWord) - 1;
		srand((double)microtime() * 100000000);
		for($i=0;$i<$endnum;$i++){ 
			$txt .= $keyWord[rand(0 , $Max)];
		}
		$this->setPassword($txt);
		return $txt;
	}
	
	function addPermission($datas)
	{
		if(is_array($datas)){
			$keys = array_keys($datas);
			for($i = 0; $i<count($keys); $i++){
				if(!$this->hasPermission($datas[$keys[$i]])){
					array_push($this->perm,$datas[$keys[$i]]);
				}
			}
		}else{
			if(!$this->hasPermission($datas)){
				array_push($this->perm,$datas)	;
			}
		}
	}
	
	function getPermissions()
	{
		return $this->perm;
	}
	
	function hasPermission($key='')
	{
		return (in_array($this->perm,$key));
	}
	
	function addRole($datas)
	{
		if(is_array($datas)){
			$keys = array_keys($datas);
			for($i = 0; $i<count($keys); $i++){
				if(!$this->hasRole($datas[$keys[$i]])){
					array_push($this->role,$datas[$keys[$i]]);
				}
			}
		}else{
			if(!$this->hasRole($datas)){
				array_push($this->role,$datas)	;
			}
		}
	}
	
	function getRoles()
	{
		return $this->role;
	}
	
	function hasRole($key='')
	{
		return (in_array($this->role,$key));
	}
	
	function getRowForDataSet(){
		
	}
}


class UserManager{
	var $context;
	var $tableName;
	var $tableColumn;
	var $tableProperty;
	
	function UserManager(&$context){
		$this->context = &$context;
		
		$this->tableName = $this->context->config['DatabaseTable']['User'];
		$this->tableColumn = $this->context->config['DatabaseColumn']['User'];
		$this->tableProperty = $this->context->config['DatabaseTableProperties']['User'];
		
	}
	
	function getUserById($id = 0){
		
		$sql = new SqlQueryBuilder($this->context,'select');
		$sql->setTable($this->tableName);
		$sql->addValue($this->tableColumn['UserID'],$id);
		
		$result = $this->context->storage->query($sql);
		if(Error::isError($result)){
			return $result;
		}
		if($result->rowCount()==1){
			$user = new User($this->context);
			$user->setRow($result->seek(1));
			return $user;
		}
		return new Error('UnknowError');
		
	}
	
	function getUserByName($name = ""){
		$sql = new SqlQueryBuilder($this->context,'select');
		$sql->setTable($this->tableName);
		$sql->addValue($this->tableColumn['UserName'],$name);
		
		
		$result = $this->context->storage->query($sql);
		if(Error::isError($result)){
			return $result;
		}
		if($result->rowCount()==1){
			$user = new User($this->context);
			$user->setRow($result->seek(1));
			return $user;
		}
		return new Error('UnknowError');
	}
	
	function getMatchedUserDataSet($requireData=false){
		if(!$requireData){
			return false;
		}
		if(!is_array($requireData)){
			return false;
		}
		
		$keys = array_keys($requireData);
		
		$sql = new SqlQueryBuilder($this->context,'select');
		$sql->setTable($this->table);
		
		for($i=0;$i<count($keys);$i++){
			$key = $keys[$i];
			if(in_array($this->tableColumn,$key)){
				$sql->addColumn($key);
				$sql->addValues($requireData[$key]);
				
			}
			if(!in_array($this->tableProperty['primary_key'],$key)){
				$sql->addColumn($key);
			}
		}
		
		$result = $this->context->storage->query($sql);
	
		if(Error::isError($result)){
			return $result;
		}
		
		return $result;
		
	}
	
	function createUser(&$user){
		
		
		
		$row = $user->getRowForDatabase();
		
		
		$cvAry = array();
		$cfAry = array();
		$rowKeys = array_keys($row);
		$fieldKeys = array_keys($field);
		for($i = 0 ; $i<count($fieldKeys); $i++){
			$fieldKey = $fieldKeys[$i];
			if(!in_array($tp['primary_key'],$field[$fieldKey])){
				array_push($cfAry,$field[$fieldKey]);
				array_push($cvAry,$row[$field[$fieldKey]]);
			}
		}
		
		$sql = "INSERT INTO $table "
		."(".implode($cfAry,', ')." )"
		." VALUES "
		."(".implode($cvAry,', ')." )"
		;
		
		
		$result = $this->context->storage->execute($sql);
		
	}
	
	function updateUser(&$user){
		$table = $this->context->config['DatabaseTable']['User'];
		$field = $this->context->config['DatabaseColumn']['User'];
		$tp = $this->context->config['DatabaseTableProperties']['User'];
		$row = $user->getRowForDatabase();
		$ucAry = array();
		$wcAry = array();
		$fieldKeys = array_keys($field);
		for($i = 0 ; $i<count($fieldKeys); $i++){
			$fieldKey = $fieldKeys[$i];
			if(in_array($tp['primary_key'],$field[$fieldKey])){
				array_push($wcAry,$field[$fieldKey]."='".$row[$field[$fieldKey]]);
			}else{
				array_push($ucAry,$field[$fieldKey]."='".$row[$field[$fieldKey]]);
			}
		}
		$sql = " UPDATE $table "
		." SET ".implode(", " , $ucAry)
		." WHERE ".implode(", " , $wcAry);
		
		$result = $this->context->storage->execute($sql);
			
	}
}
?>